package com.coocaa.aries.core.tool.reflect;

import com.coocaa.aries.core.tool.InstanceUtil;
import com.coocaa.aries.core.tool.date.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Date;
import java.util.Map;

/**
 * @author : bijiahao
 * @date : 2019/4/10.
 * @description:
 */
public class ObjectMappingUtil {
    /**
     * 获取类的 域的名称 与 域的值  存储在Map中 以供查询使用
     *
     * @param obj
     * @param params
     */
    public static void mappingObject2Map(Object obj, Map<String, Object> params) {
        try {
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                AccessController.doPrivileged(new PrivilegedAction<Object>() {
                    @Override
                    public Object run() {
                        field.setAccessible(true);
                        return null;
                    }
                });
                if (field.get(obj) instanceof String) {
                    if (StringUtils.isNotBlank(field.get(obj).toString())) {
                        params.put(field.getName(), field.get(obj));
                    }
                } else if (field.get(obj) instanceof Date) {
                    if (field.get(obj) != null) {
                        String date = DateUtil.toLongString((Date) field.get(obj));
                        params.put(field.getName(), date);
                    }
                } else {
                    if (field.get(obj) != null) {
                        params.put(field.getName(), field.get(obj));
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取类的 域的名称 与 域的值  存储在Map中 以供查询使用
     *
     * @param obj
     */
    public static Map<String, String> obj2Map(Object obj) {
        Map<String, String> result = InstanceUtil.newHashMap();
        try {
            Field[] fields = null;
            Class<?> aClass = obj.getClass();
            for (; aClass != Object.class; aClass = aClass.getSuperclass()) {
                fields = aClass.getDeclaredFields();
                handleObj2Map(obj, fields, result);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    private static void handleObj2Map(Object obj, Field[] fields, Map<String, String> params) throws IllegalAccessException {
        for (Field field : fields) {
            AccessController.doPrivileged(new PrivilegedAction<Object>() {
                @Override
                public Object run() {
                    field.setAccessible(true);
                    return null;
                }
            });
            if (field.get(obj) instanceof String) {
                if (StringUtils.isNotBlank(field.get(obj).toString())) {
                    params.put(field.getName(), ((String) field.get(obj)).intern());
                }
            } else if (field.get(obj) instanceof Date) {
                if (field.get(obj) != null) {
                    String date = DateUtil.toLongString((Date) field.get(obj));
                    params.put(field.getName(), date);
                }
            } else {
                if (field.get(obj) != null) {
                    params.put(field.getName(), field.get(obj).toString());
                }
            }
        }
    }

}
